setwd("Code/dataverse/")

library(tidyverse)



### High DL, Low DL, FB + MTurk (w/o Lucid)

q_high <- read_csv("high.csv") # high (tech)
q_low <- read_csv("low.csv") # low  (libraries)

q_fb <- read_csv("fb.csv") #  (FB ads)
q_mturk <- read_csv("mturk.csv") # (MTurk)

q_low <- q_low[, -which(colnames(q_low) %in% colnames(q_fb) == FALSE)]


## delete the first two rows
q_high<-q_high[c(-1:-2),-137]
q_low<-q_low[c(-1:-2),-137]
q_fb<-q_fb[c(-1:-2),-137]
q_mturk<-q_mturk[c(-1:-2),]




# stack

which<-c(rep("high", times = length(q_high$StartDate)), rep("low", times = length(q_low$StartDate)), 
         rep("fb", times = length(q_fb$StartDate)), rep("mturk", times = length(q_mturk$StartDate)))

q_all<-rbind(q_high, q_low, q_fb, q_mturk)

q_all$which<-which


###replace

qr<-q_all

qr<-filter(qr, Q53_1 != "")



qr$age<-substr(qr$Q3, 1, 2)

qr$age[qr$age == ";6" |qr$age == "Al" |qr$age == "Fa" |qr$age == "ol" |qr$age == "Ol" ] <-NA

qr$age<-as.numeric(qr$age)


###one person is over 90, claims to be 99...topcode to 90 for 
###ease of visualization
qr$age[qr$age > 90] <- 90

qr$internet<-as.factor(qr$Q6)
qr$internet<- factor(qr$internet,levels(qr$internet)[c(4,1,2,3,6,5)])

####internet skills

###full battery

#Phishing (1) 					
#Preference Setting (2) 					
#App (3) 					
#Hashtag (4) 					
#Social Media (5) 					
#Status Update (6) 					
#Spyware (7) 					
#Selfie (8) 					
#Wiki (9) 					
#Advanced Search (10) 					
#PDF (11) 					
#Tagging (1) 					
#Tablet (2) 					
#Smartphone (3) 					
#JPG (4) 					
#Malware (5) 					
#Cache (6) 					
#BCC (on email) (7) 					
#RSS (8) 					
#Proxypod (9) 					
#Fitibly (10) 					


qr$harg_phishing<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_1))
qr$harg_preference_setting<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_2))
qr$harg_app<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_3))
qr$harg_hashtag<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_4))
qr$harg_social_media<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_5))
qr$harg_status_update<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_6))
qr$harg_spyware<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_7))

qr$harg_selfie<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_8))
qr$harg_wiki<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_9))
qr$harg_advanced_search<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_10))
qr$harg_pdf<-as.numeric(gsub("[^0-9\\-]", "", qr$Q53_11))

qr$harg_tagging<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_1))
qr$harg_tablet<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_2))
qr$harg_smartphone<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_3))
qr$harg_jpg<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_4))
qr$harg_malware<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_5))
qr$harg_cache<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_6))
qr$harg_bcc<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_7))
qr$harg_rss<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_8))
qr$harg_proxypod<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_9))
qr$harg_fitibly<-as.numeric(gsub("[^0-9\\-]", "", qr$Q98_10))


qr$harg_mean<-(qr$harg_phishing -  qr$harg_fitibly - qr$harg_proxypod+ qr$harg_rss+ qr$harg_bcc+ qr$harg_cache+ qr$harg_malware+ qr$harg_jpg+
                 qr$harg_smartphone+ qr$harg_tablet+ qr$harg_tagging+
                 qr$harg_pdf+ qr$harg_advanced_search+ qr$harg_wiki+ qr$harg_selfie+ qr$harg_spyware+ qr$harg_status_update+ qr$harg_social_media+
                 qr$harg_preference_setting+ qr$harg_app+ qr$harg_hashtag)/21




#####power user

qr<-filter(qr, Q54_1 != "")

qr$Q54_1<-as.numeric(gsub("[^0-9\\-]", "", qr$Q54_1))
qr$Q54_2<-as.numeric(gsub("[^0-9\\-]", "", qr$Q54_2))
qr$Q54_3<-as.numeric(gsub("[^0-9\\-]", "", qr$Q54_3))
qr$Q54_4<-as.numeric(gsub("[^0-9\\-]", "", qr$Q54_4))
qr$Q54_5<-as.numeric(gsub("[^0-9\\-]", "", qr$Q54_5))
qr$Q54_6<-as.numeric(gsub("[^0-9\\-]", "", qr$Q54_6))

qr$Q99_1<-as.numeric(gsub("[^0-9\\-]", "", qr$Q99_1))
qr$Q99_2<-as.numeric(gsub("[^0-9\\-]", "", qr$Q99_2))
qr$Q99_3<-as.numeric(gsub("[^0-9\\-]", "", qr$Q99_3))
qr$Q99_4<-as.numeric(gsub("[^0-9\\-]", "", qr$Q99_4))
qr$Q99_5<-as.numeric(gsub("[^0-9\\-]", "", qr$Q99_5))
qr$Q99_6<-as.numeric(gsub("[^0-9\\-]", "", qr$Q99_6))


qr$power_mean<-(qr$Q54_2 -  qr$Q54_1+ qr$Q54_3+ qr$Q54_4+ qr$Q54_5+ qr$Q54_6)/6



qr$ask_friends<-qr$Q99_1
qr$tech_easy<-qr$Q99_2
qr$tech_daily_life<-qr$Q99_3
qr$tech_control_work<-qr$Q99_4
qr$tech_easier_work<-qr$Q99_5
qr$feel_lost_without<-qr$Q99_6


qr$tech_complicated<-qr$Q54_1
qr$use_features<-qr$Q54_2
qr$latest_tech<-qr$Q54_3
qr$replace_paper<-qr$Q54_4
qr$love_exploring<-qr$Q54_5
qr$multi_tech<-qr$Q54_6


qr$Q100_1<-as.numeric(as.factor(qr$Q100_1))
qr$Q100_2<-as.numeric(as.factor(qr$Q100_2))
qr$Q100_3<-as.numeric(as.factor(qr$Q100_3))
qr$Q100_4<-as.numeric(as.factor(qr$Q100_4))
qr$Q100_5<-as.numeric(as.factor(qr$Q100_5))

qr$low_end_mean<-(qr$Q100_1+ qr$Q100_2+ qr$Q100_3+ qr$Q100_4+ qr$Q100_5)/5


qr$rely_family<-qr$Q100_1
qr$rely_professionals<-qr$Q100_2
qr$online_confusion<-qr$Q100_3
qr$malware<-qr$Q100_4
qr$finding_files<-qr$Q100_5



####




####Code correctness


croat<-gsub(pattern = " ", "", qr$Q60)

summary(nchar(croat))

real_croat<-croat[nchar(croat)==15]

croat_a<-substr(x = croat, start= 1, stop = 1)

qr$croat_correct<-0

qr$croat_correct[croat_a == "A" | croat_a == "a" | croat_a == "P" | croat_a == "p"]<-1





qr$croat_correct[qr$Q60 == "a woman" ]<-0



malawi<-gsub(pattern = " ", "", qr$Q62)

summary(nchar(malawi))

malawi_l<-substr(x = malawi, start= 1, stop = 1)


malawi
qr$malawi_correct<-0

qr$malawi_correct[malawi_l == "L" | malawi_l == "l" ]<-1




ted<-gsub(pattern = " ", "", qr$Q92)


ted<-substr(x = ted, start= 1, stop = 4)

qr$ted_correct<-0

qr$ted_correct[ted == "Theo" | ted == "theo" | ted == "THEO" ]<-1





##drop responses with incomplete answers
qr<-filter(qr, is.na(qr$harg_mean) == FALSE)
qr<-filter(qr, is.na(qr$power_mean) == FALSE)
qr<-filter(qr, is.na(qr$low_end_mean) == FALSE)


# WRITE DATA
# write_csv(qr, file = "4_samples_cleaned.csv")





#######ADD LUCID 

q_lucid<-read_csv("lucid.csv") # (Lucid)


##delete the first 8 rows, which were test runs of the qualtrics
q_lucid<-q_lucid[c(-1:-10),]


#### Internet skills measure

#lucid:
#Selfie							
#PDF							
#App							
#Hashtag							
#Tablet							
#Smartphone							
#Fitibly


q_lucid$harg_selfie<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_1))
q_lucid$harg_pdf<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_2))
q_lucid$harg_app<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_3))
q_lucid$harg_hashtag<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_4))
q_lucid$harg_tablet<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_5))
q_lucid$harg_smartphone<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_6))
q_lucid$harg_fitibly<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q53_7))


q_lucid$harg_mean<-(q_lucid$harg_selfie  - q_lucid$harg_fitibly+ q_lucid$harg_pdf+ q_lucid$harg_app+
                 q_lucid$harg_smartphone+ q_lucid$harg_tablet+  q_lucid$harg_hashtag)/7



#####power user

q_lucid<-filter(q_lucid, Q54_1 != "")

q_lucid$Q54_1<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q54_1))
q_lucid$Q54_2<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q54_2))
q_lucid$Q54_3<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q54_3))
q_lucid$Q54_4<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q54_4))
q_lucid$Q54_5<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q54_5))
q_lucid$Q54_6<-as.numeric(gsub("[^0-9\\-]", "", q_lucid$Q54_6))






q_lucid$power_mean<-(q_lucid$Q54_2 -  q_lucid$Q54_1+ q_lucid$Q54_3+ q_lucid$Q54_4+ q_lucid$Q54_5+ q_lucid$Q54_6)/6




q_lucid$tech_complicated<-q_lucid$Q54_1
q_lucid$use_features<-q_lucid$Q54_2
q_lucid$latest_tech<-q_lucid$Q54_3
q_lucid$replace_paper<-q_lucid$Q54_4
q_lucid$love_exploring<-q_lucid$Q54_5
q_lucid$multi_tech<-q_lucid$Q54_6



q_lucid$which<-"lucid"



###add in

q_lucid <- q_lucid[, -which(colnames(q_lucid) %in% colnames(qr) == FALSE)]

qr <- qr[, -which(colnames(qr) %in% colnames(q_lucid) == FALSE)]


qr<-rbind(qr, q_lucid)

###normalize the scale of the internet skills measure


#qr$harg_mean<- 5*(qr$harg_mean - min(qr$harg_mean))/(max(qr$harg_mean)
#                                                   - min(qr$harg_mean))



qr<-filter(qr, Q6 != "")


# WRITE DATA
# write_csv(qr, file = "samples_cleaned_w_lucid.csv")




